package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.enums.CommentLevel;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.ItemsCommentsMapper;
import com.imooc.mapper.ItemsImgMapper;
import com.imooc.mapper.ItemsMapper;
import com.imooc.mapper.ItemsMapperCustom;
import com.imooc.mapper.ItemsParamMapper;
import com.imooc.mapper.ItemsSpecMapper;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsComments;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.CommentLevelCountsVO;
import com.imooc.pojo.vo.ItemCommentVO;
import com.imooc.pojo.vo.SearchItemsVO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.ItemService;
import com.imooc.utils.DesensitizationUtil;
import com.imooc.utils.PagedGridResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @ClassName: ItemServiceImpl
 * @Package: com.imooc.service.impl
 * @Description:
 * @Date: 2021/7/8 6:53
 * @Author: sunzheng@beiming.com
 */
@Service
public class ItemServiceImpl extends BaseService implements ItemService {

  @Autowired
  private ItemsMapper itemsMapper;

  @Autowired
  private ItemsImgMapper itemsImgMapper;

  @Autowired
  private ItemsSpecMapper itemsSpecMapper;

  @Autowired
  private ItemsParamMapper itemsParamMapper;

  @Autowired
  private ItemsCommentsMapper itemsCommentsMapper;

  @Autowired
  private ItemsMapperCustom itemsMapperCustom;

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public Items queryItemById(String itemId) {
    return itemsMapper.selectByPrimaryKey(itemId);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<ItemsImg> queryItemImgList(String itemId) {
    Example example = new Example(ItemsImg.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("itemId", itemId);
    List<ItemsImg> itemsImgs = itemsImgMapper.selectByExample(example);
    return itemsImgs;
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<ItemsSpec> queryItemSpecList(String itemId) {
    Example example = new Example(ItemsSpec.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("itemId", itemId);
    return itemsSpecMapper.selectByExample(example);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult searchItems(Integer catId, String sort, Integer page, Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("catId", catId);
    map.put("sort", sort);

    PageHelper.startPage(page, pageSize);

    List<SearchItemsVO> list = itemsMapperCustom.searchItems(map);
    return setPageGrid(list, page);
  }

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public void decreaseItemSpecStock(String specId, int buyCounts) {
    //synchronized 不推荐使用，集群下无用，性能低下
    //锁数据库：不推荐使用，导致数据库性能低下
    //分布式锁  zookeeper redis

    //lockUtil.getLock(); 加锁

    //1. 查询库存
//    int stock = 10;

    //判断库存是否能够减少到0以下
/*
    if (stock - buyCounts < 0) {
      //提示用户库存不够
    }
*/

    //lockUtil.unLock(); 解锁

    //在自定义sql中使用乐观锁方式实现
    int result = itemsMapperCustom.decreaseItemSpecStock(specId, buyCounts);
    if (result != 1) {
      throw new RuntimeException("订单创建失败，原因：库存不足");
    }
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public String queryItemMainImgById(String itemId) {
    ItemsImg itemsImg = new ItemsImg();
    itemsImg.setItemId(itemId);
    itemsImg.setIsMain(YesOrNo.YES.type);
    ItemsImg result = itemsImgMapper.selectOne(itemsImg);
    return result != null ? result.getUrl() : "";
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public ItemsSpec queryItemSpecById(String specId) {
    return itemsSpecMapper.selectByPrimaryKey(specId);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<ShopcartVO> queryItemsBySpecIds(String specIds) {
    String[] ids = specIds.split(",");
    List<String> specIdsList = new ArrayList<>();
    Collections.addAll(specIdsList, ids);

    return itemsMapperCustom.queryItemsBySpecIds(specIdsList);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult searchItems(String keywords, String sort, Integer page, Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("keywords", keywords);
    map.put("sort", sort);

    PageHelper.startPage(page, pageSize);

    List<SearchItemsVO> list = itemsMapperCustom.searchItems(map);
    return setPageGrid(list, page);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult queryPagedComments(String itemId, Integer level, Integer page,
      Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("itemId", itemId);
    map.put("level", level);

    /**
     * page: 第几页
     * pageSize: 每页显示条数
     */
    PageHelper.startPage(page, pageSize);

    List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);

    for (ItemCommentVO vo : list) {
      vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));
    }

    return setPageGrid(list, page);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public CommentLevelCountsVO queryCommentCounts(String itemId) {
    Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
    Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
    Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
    Integer totalCounts = goodCounts + normalCounts + badCounts;
    CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
    commentLevelCountsVO.setTotalCounts(totalCounts);
    commentLevelCountsVO.setGoodCounts(goodCounts);
    commentLevelCountsVO.setNormalCounts(normalCounts);
    commentLevelCountsVO.setBadCounts(badCounts);

    return commentLevelCountsVO;
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  public Integer getCommentCounts(String itemId, Integer level) {
    ItemsComments condition = new ItemsComments();
    condition.setItemId(itemId);
    if (level != null) {
      condition.setCommentLevel(level);
    }
    return itemsCommentsMapper.selectCount(condition);

  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public ItemsParam queryItemParam(String itemId) {
    Example example = new Example(ItemsParam.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("itemId", itemId);
    return itemsParamMapper.selectOneByExample(example);
  }
}
